package com.wyjy.bkd.base;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;

import com.wyjy.bkd.common.ActivityPageManager;
import com.wyjy.bkd.utils.SpUtils;
import com.wyjy.bkd.utils.ToastUtils;

import butterknife.ButterKnife;

/**
 * @作者: TJ
 * @时间: 2018/7/23 16:56
 * @描述:
 */
public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {


    public final String TAG = this.getClass().getSimpleName();

    protected BaseActivity mContext;
    /**
     * 页面布局的 根view
     */
    protected View         mContentView;
    /**
     * sp工具类
     */
    public    SpUtils      mSpUtils;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置不能横屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        mContext = this;
        mSpUtils = SpUtils.getInstance(mContext);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        View inflate = View.inflate(this, layoutResID, null);
        inflate.setFitsSystemWindows(true);
        setContentView(inflate);
        ButterKnife.bind(this);
        mContentView = inflate;
        //初始化页面
        init();
    }


    /**
     * 初始化页面
     */
    protected void init() {
        initData();
        initView();
        bindEvent();
    }

    /**
     * 初始化数据
     */
    protected void initData() {

    }

    /**
     * 初始化view
     */
    public abstract void initView();


    /**
     * 绑定事件
     */
    public void bindEvent() {
    }

    @Override
    public void onClick(View v) {

    }

    /**
     * 跳转页面
     *
     * @param clazz
     */
    public void skipAct(Class clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }

    public void skipAct(Class clazz, Bundle bundle) {
        Intent intent = new Intent(this, clazz);
        intent.putExtras(bundle);
        startActivity(intent);
    }


    /**
     * 显示一个Toast信息
     */
    public void showToast(String content) {
        if (content != null) {
            ToastUtils.showShort(content);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //Acitvity 释放子view资源
        ActivityPageManager.unbindReferences(mContentView);
        ActivityPageManager.getInstance().removeActivity(this);
        mContentView = null;
        ButterKnife.unbind(this);
    }
}
